package wta.mongo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
/*
 * 这个程序是将 第二批数据 02阳性上报及回访 上传到Mongo数据库中
 */
public class AddPositiveDataToMongo {
	static Mongo mongo; 
	static DB db;
	static DBCollection coll;
	static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
	
	/**
	 * 经测试，最好是先用List把数据包起来，再进行插入
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		
		mongo = new Mongo("10.110.150.226"); //配置连接
		db = mongo.getDB("china"); //选择库名
		coll = db.getCollection("positive"); //选择需要加入的表
		
		File dic  = new File("D:\\百度云同步盘\\Now\\ciming\\数据\\4ciming11w\\11w02"); //打开文件
		String[] list = dic.list(); //列出文件夹下的所有文件
		for (String string : list) {
			insert(dic.getAbsolutePath() +'\\'+ string); //依次插入每一个城市的文件
		}
		
		mongo.close(); //关闭数据库连接
	}
	
	public static void insert(String file) throws Exception {
		
		BufferedReader br = new BufferedReader(new FileReader(file)); //使用本文件默认的UTF-8来读取数据

		//get p
		int p = file.lastIndexOf('\\') + 1;
		String cityname = file.substring(p).split("\\.")[0]; 
		
		System.out.println(cityname);
		switch (cityname) {
			case "beijing":		cityname = "北京"; break;
			case "chengdu":		cityname = "成都"; break;
			case "dalian":		cityname = "大连"; break;
			case "guangzhou":	cityname = "广州"; break;
			case "jinan":		cityname = "济南"; break;
			case "nanjing":		cityname = "南京"; break;
			case "shanghai":	cityname = "上海"; break;
			case "shenzhen":	cityname = "深圳"; break;
			case "tianjin":		cityname = "天津"; break;
			case "wuhan":		cityname = "武汉"; break;
			default: break;
		}
		
		List<DBObject> objs = new ArrayList<DBObject>();
		
		while(true){
			String userInfo = br.readLine();
			if (userInfo == null) break;
			//002200111022100008$男$24$客户到医院检查，诊断为窦性心动过缓，无不适情况发生，未治疗，医嘱请关注。
			//体检号
			String uid   = userInfo.substring(0, 18);
			String ssex  = userInfo.substring(19,20);
			String sage  = userInfo.substring(21,23);
			String label = userInfo.substring(24);
			//性别
			short sex = -1; //默认-1  男  0  女 1
			if (ssex.equals("男"))
				sex = 0;
			else if (ssex.equals("女"))
				sex = 1;
			else
				System.err.println(ssex);
			//年龄
			short age = -1;
			try{
				age = Short.parseShort(sage);
			}catch(NumberFormatException e){}
			
			//执行插入操作

			BasicDBObject toFind = new BasicDBObject();
			toFind.put("uid", uid);
			DBObject obj = coll.findOne(toFind);
			if (obj == null){ //说明不存在，直接插入
				DBObject user = new BasicDBObject();
				user.put("uid", uid);
				user.put("sex", sex);
				user.put("age", age);
				user.put("city", cityname);
				user.put("label", label);
				objs.add(user);
			}else{ //说明已经存在
				System.err.println(label);
			}
			
		}
		br.close();
		System.out.println(objs.size());
		coll.insert(objs);
	}

}
